package com.mj.mafeed.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mj.mafeed.R;
import com.mj.mafeed.bean.OrderJSON;
import com.mj.mafeed.bean.ProductJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.service.ProductService;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 订单项
 * @author zhaominglei
 * @date 2014-12-16
 * 
 */
public class OrderItemAdapter extends BaseAdapter {
	private List<OrderJSON.OrderItem> orderItems;
	private Context context;
	@SuppressWarnings("unused")
	private ProgressBar progressBar;
	private ImageLoader imageLoader = null;
	private DisplayImageOptions options = null;
	private ProductService productService = new ProductService();
	
	@SuppressWarnings("deprecation")
	public OrderItemAdapter(Context context, List<OrderJSON.OrderItem> orderItems) {
		super();
		this.context = context;
		this.orderItems = orderItems;
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));

		options = new DisplayImageOptions.Builder()
				.displayer(new RoundedBitmapDisplayer(0xff000000, 10))
				.cacheInMemory().cacheOnDisc().build();
	}

	public List<OrderJSON.OrderItem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(List<OrderJSON.OrderItem> orderItems) {
		this.orderItems = orderItems;
	}
	
	@Override
	public int getCount() {
		return orderItems.size();
	}

	@Override
	public Object getItem(int position) {
		return orderItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (LinearLayout) convertView;
		} else {
			relativeLayout = (LinearLayout) View.inflate(context,
					R.layout.orderitem_item, null);
		}
		progressBar = (ProgressBar) relativeLayout.findViewById(R.id.order_item_progress);
		ImageView imageView = (ImageView) relativeLayout.findViewById(R.id.order_item_image);
		ProductJSON productJSON = productService.getProductDetail(context, orderItems.get(position).getProductId());
		imageLoader.displayImage(Constants.SERVER_DOWNLOAD_URL+productJSON.getImageUrl(),	imageView, options);
		((TextView)relativeLayout.findViewById(R.id.order_item_template_name))
				.setText(productJSON.getName());
		((TextView) relativeLayout.findViewById(R.id.order_item_template_quantity))
				.setText(String.valueOf(orderItems.get(position).getQuantity()));
		((TextView)relativeLayout.findViewById(R.id.order_item_template_price))
				.setText(String.valueOf(productJSON.getPrice()));
		((TextView)relativeLayout.findViewById(R.id.order_item_template_id))
				.setText(String.valueOf(productJSON.getProductId()));
		return relativeLayout;
	}
}
